package com.bangcommunity.bbframe.mlog.collector;

import com.bangcommunity.bbframe.common.base.attribute.AttributeHelper;
import com.bangcommunity.bbframe.common.base.attribute.CommonAttributeDesc;
import com.bangcommunity.bbframe.common.base.attribute.IAttributeAssemble;
import com.bangcommunity.bbframe.common.exception.ExceptionUtil;
import com.bangcommunity.bbframe.mlog.LogContext;
import com.bangcommunity.bbframe.mlog.logger.AsynQueuedMLogTracer;

/**
 * @author tanghc
 * @date 18/1/30
 */
public class MLogCollector {

    public static void collect(String mkey, LogContext logContext, StatisticStateEnum stateEnum, Object result,
            Throwable throwable) {
        IAttributeAssemble iAttributeAssemble = logContext.toAttributeAssemble();
        iAttributeAssemble.add(new CommonAttributeDesc("result", result != null ? result.toString() : "", true))
                .add(new CommonAttributeDesc("throwable",
                        throwable != null ? ExceptionUtil.renderToExceptionString(throwable,true) : "", true))
                .add(new CommonAttributeDesc("status", throwable != null ? "exception" : "succ", true));
        AsynQueuedMLogTracer.trace(mkey, "{}", AttributeHelper.convertToJsonString(iAttributeAssemble));

    }
}
